var {
  wechatRegist,
  getEventList,
  wechatTel,
  sendCode,
  updateInfo,
} = require('../../utils/api');
const methods = require('../../utils/methods');
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabbarlistt: app.globalData.listgl,
    list: app.globalData.indexTabBarLists,
    index: 0,//默认进来显示第一页 0
    current: 0,//默认进来显示第一个tab 0
    eventList: [],
    eveltTotal: '',
    uname:'',
    tel: '',
    code: '',
    mobile: '',
    userInfo: {},
    winWidth: 0,
    winHeight: 0,
    // tab切换
    currentTab: 0,
    orderbefor: [],
    ordering: [],
    orderover: [],
  },
  // ------------------------------------------------------------公共的方法-------------------------------------
  onLoad: function (options) {
    let _this = this;
    //获取设备的宽高
    wx.getSystemInfo({
      success: function (res) {
        _this.setData({
          clientHeight: res.windowHeight
        });
      }
    })
    if (app.globalData.userInfo) { //老用户查询
      _this.setData({
        mobile: app.globalData.mobile,
        userInfo: app.globalData.userInfo,
        uname:app.globalData.userInfo.name
      })

    } else { //新用户注册
      wechatRegist({
        "roles": 5, //5表示普通用户
        "openid": app.globalData.openid
      }).then((result) => {
        console.log('result==', result.data)
        //注册ok，直接保存用户的id
        app.globalData.userInfo = result.data;
        app.globalData.id = result.data.id;
        app.globalData.token = result.data.token
      });
    }
  },
  //最下面的tab点击事件
  tabChange(e) {
    let _this = this;
    this.setData({
      index: e.detail.index
    }, () => {
      if (e.detail.index == 1) {
        getEventList(app.globalData.id).then((res) => {
          if (res[0] == 200) {
            //刷选数据 待分配1对应待处理    待处理2对应处理中   已处理3对应已处理

            let list = res[1].content;
            let ary1 = [],
              ary2 = [],
              ary3 = [];

            for (let i = 0; i < list.length; i++) {
              if (list[i].eventStatus == 1 || list[i].eventStatus == 2) {
                ary1.push(list[i])//处理中
              } else if (list[i].eventStatus == 3 || list[i].eventStatus == 4) {
                ary2.push(list[i])//已处理 包括已驳回
              }
            }

            _this.setData({
              eventList: res[1].content,
              ordering: ary1,
              orderover: ary2,
              eveltTotal: res[1].totalElements //列表总数量
            })
          }
        })
      }
    })
  },
  //----------------------------------------------------------------tab1----------------------------------------
  //跳转到矛盾处理预约页面
  postproblem() {
    wx.navigateTo({
      url: '../personalproblem/index'
    })
  },

  //快捷方式查看我的事件-----暂时先不用
  problemlist() {
    let _this = this;
    this.setData({
      index: 1,
      current: 1
    }, () => {
      //请求接口列表
      getEventList(app.globalData.id).then((res) => {
        // console.log('列表事件==',res);
        if (res[0] == 200) {
          _this.setData({
            eventList: res[1].content,
            eveltTotal: res[1].totalElements
          })
        }
      })
    })
  },
  //跳转到新闻页面
  openInfo() {
    wx.navigateTo({
      url: '../personalnew/index'
    })
  },
  xiannum() {
    methods.showToast('正在规划中...')
    wx.navigateTo({
      url: '../personalnumber/index',
    })
  },
  otherinfo(){
    methods.showToast('正在规划中...')
  },

  //一键催办
  orderccui(){
    methods.showToast('已提交请求')
  },
  //----------------------------------------------------------------tab2----------------------------------------
  //我的事件页面-左右滑动
  bindChange: function (e) {
    console.log("滑动切换会触发的事件")
    var that = this;
    that.setData({
      currentTab: e.detail.current
    });
  },
  //点击顶部的btabbar切换
  swichNav: function (e) {
    var that = this;
    console.log('=======index====', e.target.dataset.current);
    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        currentTab: e.target.dataset.current
      })
    }
  },
  //跳转到事件详情页面--带有顶部tabbar的页面
  openorder(e) {
    console.log('111111', e.currentTarget.dataset.id);
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: "../personalorderinfo/index?id=" + id,
    })
  },
  handleTap(event){
    console.log("点击了子元素");
    methods.showToast('催办成功')
    // event.openorder();
  },
  //----------------------------------------------------------------tab3----------------------------------------
  //事件进展信息
  progress() {
    this.setData({
      index:1,
      current:1
    })
    // wx.navigateTo({
    //   url: '../personalprogress/index'
    // })
  },

  //预约记录查询
  number() {
    wx.navigateTo({
      url: '../personalnumberlist/index'
    })
  },
  new(){
    methods.showToast('业务正在拓展中...')
  },
  //操作指南
  hobbook() {
    wx.navigateTo({
      url: '../personalabout/index'
    })
  },
  //手机号码输入
  telInput(e) {
    this.setData({
      tel: e.detail.value
    })
  },
  //验证码输入
  codeInput(e) {
    this.setData({
      code: e.detail.value
    })
  },
  //获取手机验证码
  gettelcode() {
    let _this = this;
    const reg = /^[1][3,4,5,6,7,8,9][0-9]{9}$/
    console.log(_this.data.tel, app.globalData.openid)

    if (!reg.test(_this.data.tel)) {
      methods.showToast('手机号码格式不对')
      return;
    }
    //发送验证码
    sendCode(_this.data.tel).then((res) => {
      console.log('短信===', res)
      if (res.data[0] == 200) {
        methods.showToast('验证码已发送')
        _this.setData({
          code: res[1]
        })
      }
    })

  },

  //授权登录接口
  userlogin() {
    let _this = this;
    console.log('tel==', _this.data.tel);
    console.log('code==', _this.data.code);
    console.log('openid==', app.globalData.openid);
    if (!_this.data.code) {
      methods.showToast('验证码不为空')
      return;
    }
    wechatTel({
      tel: _this.data.tel,
      code: _this.data.code,
      openid: app.globalData.openid
    }).then((res) => {
      console.log('res==', res);
      if (res.data[0] == 200) {
        //绑定成功后返回上个页面
        methods.showToast('绑定成功')
        app.globalData.mobile = _this.data.tel;
        _this.setData({
          mobile: _this.data.tel
        }, () => {
          _this.onLoad();
          console.log('=====', _this.data.mobile)
        })
        //更新页面
      } else {
        methods.showToast(res.data[1].msg)
      }

    })
  },
  //获取用户信息
  getUserProfile(e) {
    let _this = this;
    wx.getUserProfile({
      desc: '展示用户信息',
      success: (res) => {
        console.log(res.userInfo)
        console.log(app.globalData.userInfo);
        //上传头像和昵称
        updateInfo({
          "id": app.globalData.id,
          "nickname": res.userInfo.nickName,
          "avatarurl": res.userInfo.avatarUrl
        }).then((res) => {
          console.log(res);
          if (res[0] == 200) { //插入成功
            // wx.redirectTo({
            //   url: `/pages/home/index`
            // })
            // wx.navigateBack()
          }
        })

        app.globalData.userInfo.nickname = res.userInfo.nickName
        app.globalData.userInfo.avatarurl = res.userInfo.avatarUrl
        console.log('2=====', app.globalData.userInfo)
        _this.setData({
          userInfo: app.globalData.userInfo,
        })
      }
    })
  },
  // 用户选择头像
  onChooseAvatar(e) {
    const {
      avatarUrl
    } = e.detail
    console.log('=====', JSON.stringify(avatarUrl));
    console.log(e.detail);
    // this.setData({
    //   ['userInfo.avatarUrl']: avatarUrl,
    // })
    // app.globalData.userimg = avatarUrl
  },

  // 用户修改昵称
  changeNickName(e) {
    let name = e.detail.value;
    if (name.length === 0) return;
    this.setData({
      ['userInfo.nickName']: e.detail.value
    })
    // app.globalData.nickname = e.detail.value
    console.log(e.detail.value)
  },
  //更换头像
  getAvatarUrl() {

  },

})